﻿using Hangfire;
using Hangfire.Common;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;

namespace HangfireTaskDemo
{
    public class Startup
    {
        /// <summary>
        /// Startup
        /// </summary>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// Configuration
        /// </summary>
        public IConfiguration Configuration { get; }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
        {
            app.UseRouting();
            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            {
                DashboardTitle = "测试任务",
                DisplayStorageConnectionString=true
            });

            //RecurringJob.AddOrUpdate<ITestMainService>("mainjob", service => service.ExecuteAsync("执行1："), "0/5 * * * *");
            //RecurringJob.AddOrUpdate<ITestMainService>("subjob", service => service.ExecuteAsync("执行："), "0/1 * * * *");
        }

        /// <summary>
        /// ConfigureServices
        /// </summary>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<ITestMainService, TestMainService>();
            services.AddTransient<ITestSubService, TestSubService>();
            services.AddHangfire(globalConfig =>
            {
                globalConfig.UseLogProvider(new NoLogProvicer());
                globalConfig.UseSqlServerStorage("Server=(localdb)\\MSSQLLocalDB;Initial Catalog=AiQiuQuanDemo_db;Integrated Security=true;");
            });
            services.AddHangfireServer(option =>
            {
                option.WorkerCount = 2;
                option.TimeZoneResolver = new LocalTimeZoneResolver();
            });
        }
    }
}
